import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Work, WorkStatus } from './work.entity';
import { Repository, LessThan } from 'typeorm';
import { ProjectService } from 'src/project/project.service';
import { Http } from 'src/util/http';
import { UserSys } from 'src/user/userSys.entity';
import { Cron, CronExpression } from '@nestjs/schedule';

@Injectable()
export class WorkService {
  private readonly logger = new Logger(WorkService.name);

  constructor(
    @InjectRepository(Work)
    public readonly work: Repository<Work>, // private readonly projectService: ProjectService
  ) {}

  async getRank(projectid: number) {
    const works = await this.work.find({
      where: {
        project: {
          id: projectid,
        },
      },
      relations: ['user', 'user.user', ''],
    });
  }

  @Cron('0 4 * * *') // 每天凌晨4点执行
  async cleanupRejectedWorks() {
    try {
      const threeDaysAgo = new Date();
      threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);

      const result = await this.work.delete({
        status: WorkStatus.reject,
        updateAt: LessThan(threeDaysAgo),
      });

      this.logger.log(`清理了 ${result.affected} 条3天前的拒绝状态工作记录`);
    } catch (error) {
      this.logger.error('清理拒绝状态工作记录时发生错误:', error);
    }
  }
}
